package com.LJ.handler;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.weixin.exception.WeixinException;

/**
 * 全局异常处理
 * 
 * @author huangwenbin
 *
 */
@ControllerAdvice
public class ExceptionHandler {

	/**
	 * 传入参数异常
	 * 
	 * @param e
	 * @return
	 */
	@ResponseBody
	@ResponseStatus(value = HttpStatus.BAD_REQUEST)
	@org.springframework.web.bind.annotation.ExceptionHandler(MissingServletRequestParameterException.class)
	public String handleUnexpectedServerError(Exception e) {
		e.printStackTrace();
		return e.getMessage();
	}

	/**
	 * 捕获微信异常
	 * 
	 * @param e
	 * @return
	 */
	@ResponseBody
	@org.springframework.web.bind.annotation.ExceptionHandler(WeixinException.class)
	public String handleWeixinException(WeixinException e) {
		e.printStackTrace();
		return e.getMessage();
	}

	/**
	 * 捕获所有非运行异常
	 * 
	 * @return
	 */
	@ResponseBody
	@org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
	public String handleException(Exception e) {
		e.printStackTrace();
		return e.getMessage();
	}

	/**
	 * 系统错误
	 * 
	 * @param ex
	 * @return
	 */
	@org.springframework.web.bind.annotation.ExceptionHandler(RuntimeException.class)
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ResponseBody
	public String handleUnexpectedServerError(RuntimeException ex) {
		return ex.getMessage();
	}
}
